package com.therandoms.rtgdroid.steps;

import java.util.ArrayList;

import org.codepond.wizardroid.WizardStep;
import org.codepond.wizardroid.persistence.ContextVariable;
import org.therandoms.textgen.TextGeneratorDictionary;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.therandoms.rtgdroid.R;

/**
 * The final step: making and displaying random text.
 * 
 * @author Logan Widick
 * @author Vinh Pham
 */
public class RandomTextResultsStep extends WizardStep {

	/**
	 * The grammar object itself
	 */
	@ContextVariable
	private TextGeneratorDictionary<String> grammar;

	/**
	 * The list of desired production categories
	 */
	@ContextVariable
	private ArrayList<String> desiredProductionCategories;

	/**
	 * Stores the result of the random generation.
	 */
	@ContextVariable
	private ArrayList<String> outList;

	/**
	 * Default # of recurrences
	 */
	private static final int DEFAULT_RECURRENCES = 5;

	/**
	 * The results view.
	 */
	private TextView resultsView = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.step_random_text_results, container,
				false);
		resultsView = (TextView) v.findViewById(R.id.textResultsView);

		if (outList.size() <= 0) {
			startRandomGeneration();
		} else {
			final StringBuffer outBuf = new StringBuffer();
			for (String word : outList) {
				outBuf.append(word);
				outBuf.append(" ");
			}
			resultsView.setText(outBuf);
		}

		return v;
	}

	private void startRandomGeneration() {
		SharedPreferences mPrefs = PreferenceManager
				.getDefaultSharedPreferences(getActivity());
		
		//get number of recurrences
		int numRecurrences = Integer.parseInt(mPrefs.getString(
				"com.therandoms.rtgdroid.num_recurrences",
				String.valueOf(DEFAULT_RECURRENCES)));
		if (numRecurrences <= 0) {
			numRecurrences = DEFAULT_RECURRENCES;
		}
		final int recurrenceCount = numRecurrences;

		// do we randomly handle production categories as terminal symbols?
		final boolean randomlyHandleCategory = mPrefs.getBoolean(
				"com.therandoms.rtgdroid.randomly_handle_production_category",
				false);

		new Thread(new Runnable() {

			@Override
			public void run() {
				// make the text
				grammar.generateText(desiredProductionCategories, outList,
						randomlyHandleCategory, recurrenceCount);

				final StringBuffer outBuf = new StringBuffer();
				for (String word : outList) {
					outBuf.append(word);
					outBuf.append(" ");
				}

				// show the text on the ui thread.
				(new Handler(Looper.getMainLooper())).post(new Runnable() {

					@Override
					public void run() {
						resultsView.setText(outBuf.toString());
					}
				});

			}
		}).start();
	}
}
